package com.zuxia.impDao;

import java.sql.SQLException;
import java.util.List;

import javax.persistence.Table;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.zuxia.iDao.InventoryinfoDAOI;
import com.zuxia.po.Inventoryinfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * Inventoryinfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.zuxia.impDao.Inventoryinfo
 * @author MyEclipse Persistence Tools
 */
@Transactional
public class InventoryinfoDAO extends HibernateDaoSupport implements InventoryinfoDAOI {
	private static final Logger log = LoggerFactory
			.getLogger(InventoryinfoDAO.class);
	// property constants
	public static final String ITINAME = "itiname";
	public static final String ITIPRICE = "itiprice";
	public static final String ITISELLING = "itiselling";
	public static final String ITIPICTURE = "itipicture";
	public static final String ITIPARAMETER = "itiparameter";
	public static final String ITIEFFECT = "itieffect";
	public static final String ITICOUNT = "iticount";

	protected void initDao() {
		// do nothing
	}

	public void save(Inventoryinfo transientInstance) {
		log.debug("saving Inventoryinfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	/*
	 * 查询总数
	 * */
	public Integer count(String name) {
		log.debug("sel ALL Staffindo For PageCount");
		StringBuffer hql = new StringBuffer("select count(*) from Inventoryinfo");
		if(!name.equals("")) {
			hql.append(" where itiname like "+"'%"+name+"%'");
		}
		long id=(Long) getHibernateTemplate().iterate(hql.toString()).next();
		return (int)id;
	}

	public void delete(Inventoryinfo persistentInstance) {
		log.debug("deleting Inventoryinfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("delete failed", re);
			throw re;
		}
	}

	public List findById(int id) {
		log.debug("getting Inventoryinfo instance with id: " + id);
		try {
			String queryString = "from Inventoryinfo where itiid="+id;
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByExample(Inventoryinfo instance) {
		log.debug("finding Inventoryinfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InventoryinfoDAOI#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Inventoryinfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Inventoryinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InventoryinfoDAOI#findByItiname(java.lang.Object)
	 */
	public List findByItiname(Object itiname) {
		return findByProperty(ITINAME, itiname);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InventoryinfoDAOI#findByItiprice(java.lang.Object)
	 */
	public List findByItiprice(Object itiprice) {
		return findByProperty(ITIPRICE, itiprice);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InventoryinfoDAOI#findByItiselling(java.lang.Object)
	 */
	public List findByItiselling(Object itiselling) {
		return findByProperty(ITISELLING, itiselling);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InventoryinfoDAOI#findByItipicture(java.lang.Object)
	 */
	public List findByItipicture(Object itipicture) {
		return findByProperty(ITIPICTURE, itipicture);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InventoryinfoDAOI#findByItiparameter(java.lang.Object)
	 */
	public List findByItiparameter(Object itiparameter) {
		return findByProperty(ITIPARAMETER, itiparameter);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InventoryinfoDAOI#findByItieffect(java.lang.Object)
	 */
	public List findByItieffect(Object itieffect) {
		return findByProperty(ITIEFFECT, itieffect);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InventoryinfoDAOI#findByIticount(java.lang.Object)
	 */
	public List findByIticount(Object iticount) {
		return findByProperty(ITICOUNT, iticount);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.InventoryinfoDAOI#findAll()
	 */
	public List findAll() {
		log.debug("finding all Inventoryinfo instances");
		try {
			String queryString = "from Inventoryinfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List<Inventoryinfo> selAllInventoryinfoPageModel(Integer page, Integer tr, String itiname) {
		if(itiname=="" || itiname==null){
			Query query = this.getSession().createQuery("from Inventoryinfo");
			Integer pageStart = (page - 1) * tr;
			//起始数据
			 query.setFirstResult(pageStart);
			 //每页的条数
			 query.setMaxResults(tr);
			  List<Inventoryinfo> list = query.list();
			  return list;
		}else{
			 Query query = this.getSession().createQuery("from Inventoryinfo where itiname like '%" + itiname + "%'");
			 Integer pageStart = (page - 1) * tr;
			 query.setFirstResult(pageStart);
	         query.setMaxResults(tr);
	         List<Inventoryinfo> list = query.list();
	          return list;
		}
	}
	

	public Inventoryinfo merge(Inventoryinfo detachedInstance) {
		log.debug("merging Inventoryinfo instance");
		try {
			Inventoryinfo result = (Inventoryinfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Inventoryinfo instance) {
		log.debug("attaching dirty Inventoryinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Inventoryinfo instance) {
		log.debug("attaching clean Inventoryinfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static InventoryinfoDAOI getFromApplicationContext(
			ApplicationContext ctx) {
		return (InventoryinfoDAOI) ctx.getBean("InventoryinfoDAO");
	}

	public Boolean delectInventoryinfo(int itiid) {
		// TODO Auto-generated method stub
		Boolean b=true;
		log.debug("deleting Inventoryinfo instance");
		try {
			Inventoryinfo i=(Inventoryinfo) getHibernateTemplate().getSessionFactory().getCurrentSession().get(Inventoryinfo.class, itiid);
			getHibernateTemplate().delete(i);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			b=false;
			re.printStackTrace();
			log.error("delete failed", re);
			throw re;
		}
		return b;
	}

	public boolean updateInventoryinfo(Inventoryinfo transientInstance) {
		boolean b=true;
				try {
					getHibernateTemplate().update(transientInstance);
					return true;
				} catch (Exception e) {
					b=false;
					// TODO: handle exception
					System.out.println(e.getMessage());
					System.out.println(e.getStackTrace());
				}
				return b;
	}

	
}